//
//  OSTopicCell.m
//  百思不得姐
//
//  Created by PushMeBaby on 16/3/2.
//  Copyright © 2016年 CoderC. All rights reserved.
//

#import "OSTopicCell.h"
#import "OSTopic.h"
#import "OSTopicVideoView.h"
#import "OSTopicVoiceView.h"
#import "OSTopicPicture.h"

@interface OSTopicCell ()



@property (weak, nonatomic) IBOutlet UIImageView *headImage;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *text_Label;

@property (weak, nonatomic) IBOutlet UIButton *ding;

@property (weak, nonatomic) IBOutlet UIButton *cai;
@property (weak, nonatomic) IBOutlet UIButton *share;
@property (weak, nonatomic) IBOutlet UIButton *commentButton;
@property (nonatomic,weak)OSTopicPicture *pictureView;
@property (nonatomic,weak)OSTopicVoiceView *voiceView;


@end


@implementation OSTopicCell


+(instancetype)cell{
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil ]lastObject];
}

-(OSTopicPicture *)pictureView{
    if (!_pictureView) {
        OSTopicPicture * pictureView = [OSTopicPicture pictureView];
        [self.contentView addSubview:pictureView];
        _pictureView = pictureView;
    }
    return _pictureView;
}
-(OSTopicVoiceView *)voiceView{
    if (!_voiceView) {
        OSTopicVoiceView *voiceView = [OSTopicVoiceView voiceView];
        [self.contentView addSubview:voiceView];
        _voiceView = voiceView;
    }
    return _voiceView;
}
-(OSTopicVideoView *)videoView{
    if (!_videoView) {
        OSTopicVideoView *videoView = [OSTopicVideoView videoView];
        [self.contentView addSubview:videoView];
        _videoView = videoView;
    }
    return _videoView;
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)setTopic:(OSTopic *)topic{
    _topic = topic;
    
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:topic.profile_image] placeholderImage:[UIImage imageNamed:@"defaultUserIcon"]];
    
    self.nameLabel.text = topic.name;
    

    [self setupButtonTitle:self.ding count:topic.ding placeholder:@"顶"
     ];
    [self setupButtonTitle:self.cai count:topic.cai placeholder:@"踩"
     ];
    [self setupButtonTitle:self.share count:topic.ding placeholder:@"分享"
     ];
    [self setupButtonTitle:self.commentButton count:topic.ding placeholder:@"评论"
     ];
    self.text_Label.text = topic.text;
    
    self.timeLabel.text = topic.create_time;
    
    
    if (topic.type == OSTopicTypePicture) {
        
        self.pictureView.hidden = NO;
        self.pictureView.topic = topic;
        self.pictureView.frame = topic.pictureF;
        
        self.voiceView.hidden = YES;
        self.videoView.hidden = YES;
    }else if (topic.type == OSTopicTypeVoice){
        self.voiceView.hidden = NO;
        self.voiceView.topic = topic;
        self.voiceView.frame = topic.voiceF;
    }else if (topic.type == OSTopicTypeVideo){
        
        self.videoView.hidden = NO;
        self.videoView.topic = topic;
        self.videoView.frame = topic.videoF;
        
        self.voiceView.hidden = YES;
        self.pictureView.hidden = YES;
    }else{
        
        self.videoView.hidden = YES;
        self.voiceView.hidden = YES;
        self.pictureView.hidden = YES;
    }
    
    
    
    
}
-(void)setupButtonTitle:(UIButton *)button count:(NSInteger)count placeholder:(NSString *)placeholder{
    if (count > 10000) {
        placeholder = [NSString stringWithFormat:@"%.1f万",count / 10000.0];
    }else if (count > 0){
        placeholder = [NSString stringWithFormat:@"%zd",count];
    }
    
    [button setTitle:placeholder forState:UIControlStateNormal];
}

-(void)setFrame:(CGRect)frame{
    frame.origin.x = OSTopicCellMargin;
    frame.size.width -= 2 * OSTopicCellMargin;
    frame.size.height -= OSTopicCellMargin;
    frame.origin.y += OSTopicCellMargin;
    
    [super setFrame:frame];
}

@end
